const FORM_DATA_BOUNDARY = 'hand-in-hand-e302c658-80aa-42d0-5997-d9f9asdj23f3b78'
const BASE_URL = 'https://admin.slsxh.com/api'
// const BASE_URL = 'http://39.100.80.73/api'
// const UPLOAD_URL = 'https://applet.jufazu.com.cn/api/index/uploadImage'
// const UPLOAD_URL = 'http://39.100.80.73/api/pub/uploadImage'
const UPLOAD_URL = 'https://admin.slsxh.com/api/pub/uploadImage'
/**
 * 转换传统JSON格式请求体为form-data
 */
function ConvertJSON2FormData(data: object) {
  let formData = ''
  function buildFormItem(key: string, value: string | number | null) {
    let resultString = ''
    resultString += `\r\n--${FORM_DATA_BOUNDARY}`
    resultString += `\r\nContent-Disposition: form-data; name="${key}"`
    resultString += `\r\n`
    resultString += `\r\n${value}`
    return resultString
  }
  const keys = Object.keys(data)
  keys.forEach((key) => {
    formData += buildFormItem(key, data[key])
  })
  formData += `\r\n--${FORM_DATA_BOUNDARY}--`

  return formData
}

class Request {
  baseUrl: string

  constructor(baseUrl: string) {
    this.baseUrl = baseUrl
  }

  get(url: string, param: object) {
    const self = this
    return new Promise((resolve, reject) => {
      wx.request({
        header: { Authorization: wx.getStorageSync('token') },
        method: 'GET',
        url: self.baseUrl + url,
        data: param,
        success: function (result) {
          resolve(result.data)
        },
        fail: function (error) {
          reject(error)
        },
      })
    })
  }

  post(url: string, data: object) {
    const self = this
    return new Promise((resolve, reject) => {
      wx.request({
        header: {
          Authorization: wx.getStorageSync('token'),
          'Content-Type': `multipart/form-data;boundary=${FORM_DATA_BOUNDARY}`,
        },
        method: 'POST',
        url: self.baseUrl + url,
        data: ConvertJSON2FormData(data),
        success: function (result) {
          if (result.data.code === 10006) {
            wx.removeStorageSync('open_id')
            wx.removeStorageSync('userInfo')
            wx.removeStorageSync('phone')
            wx.removeStorageSync('token')
            // wx.showToast({
            //   icon:'none',
            //   title:result.data.msg
            // })
            // resolve(null)
          }
          resolve(result.data)
        },
        fail: function (error) {
          reject(error)
        },
      })
    })
  }
}

const UploadFileUtil = (image: string) => {
  if (!image) {
    throw new Error('请传入图片资源参数')
  }

  return new Promise((resolve, reject) => {
    wx.uploadFile({
      url: UPLOAD_URL,
      filePath: image,
      name: 'file',
      header: {
        // 'content-type': 'multipart/form-data', // 设置请求头
        Authorization: wx.getStorageSync('token'),
      },
      success(uploadResult) {
        let data = JSON.parse(uploadResult.data)
        resolve(data.data)
      },
      fail(err) {
        reject(err)
      },
    })
  })
}

const RequestINstance = new Request(BASE_URL)

export default RequestINstance
export { UploadFileUtil }
